package com.codrim.cs.data.server.rpc;

import com.alibaba.dubbo.config.annotation.Service;
import com.codrim.cs.data.rpc.ReportService;
import com.codrim.cs.data.rpc.dto.param.ReportStatisticParamDTO;
import com.codrim.cs.data.server.dao.mapper.StatCsAdvAffMapper;
import com.codrim.cs.data.server.dao.mapper.StatCsAdvAllMapper;
import com.codrim.cs.data.server.dao.mapper.StatCsAdvOfferMapper;
import com.codrim.cs.data.server.dao.mapper.StatCsAffAllMapper;
import com.codrim.cs.data.server.dao.mapper.StatCsAffOfferMapper;
import com.codrim.cs.data.server.dao.mapper.StatCsGeneralMapper;
import com.codrim.cs.data.server.subservice.ReportSubService;
import com.codrim.cs.data.server.subservice.TimeZoneSubService;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * Implementation for {@link ReportService}
 *
 * @author Liang.Zhuge on 13/07/2017.
 */
@Service(version = ReportService.RPC_VERSION)
public class ReportServiceImpl implements ReportService {
    private static final Logger logger = LoggerFactory.getLogger(ReportServiceImpl.class);

    @Resource private StatCsGeneralMapper statCsGeneralMapper;
    @Resource private TimeZoneSubService timeZoneSubService;

    @Resource private StatCsAffAllMapper statCsAffAllMapper;
    @Resource private StatCsAdvAllMapper statCsAdvAllMapper;
    @Resource private StatCsAdvAffMapper statCsAdvAffMapper;
    @Resource private StatCsAdvOfferMapper statCsAdvOfferMapper;
    @Resource private StatCsAffOfferMapper statCsAffOfferMapper;
    @Resource private ReportSubService reportSubService;

    @Override
    public void statisticDailyReport(ReportStatisticParamDTO param) {
        logger.info("Statistic daily report, param:{}", param);
        reportSubService.doDailyReportStatistic(param);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void deleteSpecifiedDayReportData(String yyyyMMdd, int zone) {
        Assert.isTrue(StringUtils.isNotBlank(yyyyMMdd) && yyyyMMdd.length() == 8 && NumberUtils.isCreatable(yyyyMMdd));
        String sZone = timeZoneSubService.prefixTableNameForTimeZone(zone);
        int sdate = NumberUtils.toInt(yyyyMMdd);
        statCsGeneralMapper.deleteSpecifiedDayReportData(sdate, sZone);
        statCsAffOfferMapper.deleteSpecifiedDayReportData(sdate, sZone);
        statCsAdvOfferMapper.deleteSpecifiedDayReportData(sdate, sZone);
        statCsAdvAffMapper.deleteSpecifiedDayReportData(sdate, sZone);
        statCsAdvAllMapper.deleteSpecifiedDayReportData(sdate, sZone);
        statCsAffAllMapper.deleteSpecifiedDayReportData(sdate, sZone);
    }


}
